﻿// Decompiled with JetBrains decompiler
// Type: TaleWorlds.CampaignSystem.Settlements.Buildings.Building
// Assembly: TaleWorlds.CampaignSystem, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: E85F8C15-4DF6-4E9C-A58A-29177E40D07A
// Assembly location: D:\steam\steamapps\common\Mount & Blade II Bannerlord\bin\Win64_Shipping_Client\TaleWorlds.CampaignSystem.dll

using System.Collections.Generic;
using TaleWorlds.Library;
using TaleWorlds.Localization;
using TaleWorlds.SaveSystem;

#nullable disable
namespace TaleWorlds.CampaignSystem.Settlements.Buildings
{
  public class Building
  {
    [SaveableField(0)]
    public readonly BuildingType BuildingType;
    [SaveableField(1)]
    public float BuildingProgress;
    public const float MaxHitpoints = 100f;
    [SaveableField(2)]
    public bool IsCurrentlyDefault;
    [SaveableField(3)]
    private int _currentLevel;
    [SaveableField(5)]
    private float _hitpoints = 100f;

    internal static void AutoGeneratedStaticCollectObjectsBuilding(
      object o,
      List<object> collectedObjects)
    {
      ((Building) o).AutoGeneratedInstanceCollectObjects(collectedObjects);
    }

    protected virtual void AutoGeneratedInstanceCollectObjects(List<object> collectedObjects)
    {
      collectedObjects.Add((object) this.BuildingType);
      collectedObjects.Add((object) this.Town);
    }

    internal static object AutoGeneratedGetMemberValueTown(object o)
    {
      return (object) ((Building) o).Town;
    }

    internal static object AutoGeneratedGetMemberValueBuildingType(object o)
    {
      return (object) ((Building) o).BuildingType;
    }

    internal static object AutoGeneratedGetMemberValueBuildingProgress(object o)
    {
      return (object) ((Building) o).BuildingProgress;
    }

    internal static object AutoGeneratedGetMemberValueIsCurrentlyDefault(object o)
    {
      return (object) ((Building) o).IsCurrentlyDefault;
    }

    internal static object AutoGeneratedGetMemberValue_currentLevel(object o)
    {
      return (object) ((Building) o)._currentLevel;
    }

    internal static object AutoGeneratedGetMemberValue_hitpoints(object o)
    {
      return (object) ((Building) o)._hitpoints;
    }

    public TextObject Name => this.BuildingType.Name;

    public TextObject Explanation => this.BuildingType.Explanation;

    [SaveableProperty(6)]
    public Town Town { get; private set; }

    public int CurrentLevel
    {
      get => this._currentLevel;
      set
      {
        this._currentLevel = value;
        if (this.Town.Owner == null)
          return;
        this.Town.Owner.SetLevelMaskIsDirty();
      }
    }

    public Building(
      BuildingType buildingType,
      Town town,
      float buildingProgress = 0.0f,
      int currentLevel = 0)
    {
      this.BuildingType = buildingType;
      this.BuildingProgress = buildingProgress;
      this.Town = town;
      this._currentLevel = currentLevel;
      this.IsCurrentlyDefault = false;
      int num = buildingType.IsDefaultProject ? 1 : 0;
    }

    public override int GetHashCode() => this.BuildingType.GetHashCode() + this.Town.GetHashCode();

    public int GetConstructionCost()
    {
      float num = 1f;
      if (this.Town.Settlement.OwnerClan.Kingdom != null && this.Town.Settlement.OwnerClan.Kingdom.ActivePolicies.Contains(DefaultPolicies.CastleCharters))
        num = 0.8f;
      return (int) ((double) this.BuildingType.GetProductionCost(this._currentLevel) * (double) num);
    }

    public void LevelUp()
    {
      if (this.CurrentLevel >= 3)
        return;
      int constructionCost = this.GetConstructionCost();
      ++this.CurrentLevel;
      this.BuildingProgress -= (float) constructionCost;
      CampaignEventDispatcher.Instance.OnBuildingLevelChanged(this.Town, this, 1);
    }

    public void LevelDown()
    {
      if (this.CurrentLevel == this.BuildingType.StartLevel)
        return;
      --this.CurrentLevel;
      this.BuildingProgress = 0.0f;
      this._hitpoints = 100f;
      CampaignEventDispatcher.Instance.OnBuildingLevelChanged(this.Town, this, -1);
    }

    public void HitPointChanged(float change)
    {
      if (this.CurrentLevel == this.BuildingType.StartLevel)
        return;
      this._hitpoints = MathF.Clamp(this._hitpoints + change, 0.0f, 100f);
      if ((double) this._hitpoints != 0.0)
        return;
      this.LevelDown();
    }

    public float GetBuildingEffectAmount(BuildingEffectEnum effect)
    {
      if (this._currentLevel < this.BuildingType.StartLevel || this._currentLevel > 3)
        Debug.FailedAssert("Building: " + (object) this.Name + " current level is out of bounds!", "C:\\Develop\\MB3\\Source\\Bannerlord\\TaleWorlds.CampaignSystem\\Settlements\\Buildings\\Building.cs", nameof (GetBuildingEffectAmount), 126);
      return this._currentLevel == 0 ? 0.0f : Campaign.Current.Models.BuildingEffectModel.GetBuildingEffectAmount(this, effect);
    }

    public TextObject GetBonusExplanation()
    {
      return this._currentLevel == 0 ? TextObject.Empty : this.GetBonusExplanations()[this._currentLevel - 1];
    }

    private TextObject[] GetBonusExplanations()
    {
      TextObject[] bonusExplanations = new TextObject[3]
      {
        TextObject.Empty,
        TextObject.Empty,
        TextObject.Empty
      };
      if (this._currentLevel == 0 || this._currentLevel > 3)
        return bonusExplanations;
      for (int level = 0; level < this._currentLevel; ++level)
        bonusExplanations[level] = this.BuildingType.GetExplanationAtLevel(level);
      return bonusExplanations;
    }
  }
}
